package com.yex.service.otc.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yex.utils.AutoTradeUtils;
import com.yex.utils.JsonResultVo;
import com.yex.entity.otc.OtcCoin;
import com.yex.dao.otc.OtcCoinDao;
import com.yex.service.otc.IOtcCoinService;
import com.yex.utils.vo.TickerVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * otc 法币列表 服务实现类
 * </p>
 *
 * @author Little Liu
 * @since 2018-05-03
 */
@Service
public class OtcCoinServiceImpl extends ServiceImpl<OtcCoinDao, OtcCoin> implements IOtcCoinService {


    @Override
    public JsonResultVo otcCoinList() {
        List<OtcCoin> otcCoinList = baseMapper.findOtcCoinList();
        return new JsonResultVo(true, "获取成功", otcCoinList, 0);
    }

    @Override
    public JsonResultVo findOtcCoin(Integer coinId) {
        return new JsonResultVo(true, "获取成功", baseMapper.selectById(coinId), 0);
    }

    @Override
    public JsonResultVo findOtcCoinFvId(Integer fvId) {
        Map<String, Object> paraMap = new HashMap<>();
        paraMap.put("coinId", fvId);
        return new JsonResultVo(true, "获取成功", baseMapper.selectByMap(paraMap), 0);
    }

    @Override
    @Transactional
    public void updateOtcCoinPrize() {
        List<OtcCoin> otcCoinList = baseMapper.findOtcCoinList();
        for (OtcCoin otcCoin : otcCoinList) {
            TickerVO okcoinTicker = AutoTradeUtils.getOkcoinTicker(otcCoin.getCoinName().toLowerCase() + "_usdt");
            TickerVO binanceTicker = AutoTradeUtils.getBinanceTicker(otcCoin.getCoinName().toUpperCase() + "USDT");
            TickerVO huobiTicker = AutoTradeUtils.getHuobiTicker(otcCoin.getCoinName().toLowerCase() + "usdt");
            //TickerVO otcLastPriceTicker = redisService.getOtcLastPriceTicker(otcCoin.getId(), AutoIndexEnum.COINMARKETCAP);
            TickerVO otcLastPriceTicker=null;
            TickerVO bitfinexTicker = AutoTradeUtils.getBitfinexTicker(otcCoin.getCoinName().toUpperCase() + "USD");
            if (okcoinTicker != null) {
                otcCoin.setOkcoinPrize(okcoinTicker.getLast());
            }
            if (binanceTicker != null) {
                otcCoin.setBinancePrize(binanceTicker.getLast());
            }
            if (huobiTicker != null) {
                otcCoin.setHuobiPrize(huobiTicker.getLast());
            }
            if (otcLastPriceTicker != null) {
                otcCoin.setCoinmarketcapPrize(otcLastPriceTicker.getLast());
            }
            if (bitfinexTicker != null) {
                otcCoin.setBitfinexPrize(bitfinexTicker.getLast());
            }
            otcCoin.setModifyDate(new Date());
            updateById(otcCoin);
        }
    }


}
